package com.yvtian.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yvtian.admin.pojo.SysLog;
import com.yvtian.admin.pojo.SysUser;
import com.yvtian.admin.service.SysLogService;
import com.yvtian.annotation.Log;
import com.yvtian.annotation.PreAuthorize;
import com.yvtian.constants.LogConstant;
import com.yvtian.entity.Result;
import com.yvtian.entity.StatusCode;
import com.yvtian.enums.OperatorType;
import com.yvtian.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 日志操作
 */
@RestController
@RequestMapping(value = "api/admin/log")
public class LogController {

    @Autowired
    private SysLogService SysLogService;

    //不使用@RequestBody，前端就应该使用params:{}携带参数
    @Log(value = "获取操作日志")
    @PreAuthorize(hasPermission = "operlog:list")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result findByPage(Page page, SysLog sysLog) {
        return new Result(true, "查询成功", StatusCode.OK, SysLogService.findByPage(page, sysLog));
    }

    @Log(value = "删除操作日志", isSave = true, operType = OperatorType.DELETE)
    @PreAuthorize(hasPermission = "operlog:del")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result del(@RequestBody Map<String, ArrayList<Integer>> map) {
        ArrayList<Integer> ids = map.get("ids");
        SysLogService.del(ids);
        return new Result(true, "删除成功", StatusCode.OK);
    }

    @Log(value = "清空操作日志", isSave = true, operType = OperatorType.CLEAR)
    @PreAuthorize(hasPermission = "operlog:clear")
    @RequestMapping(value = "/clear", method = RequestMethod.POST)
    public Result clear() {
        SysLogService.clear();
        return new Result(true, "删除成功", StatusCode.OK);
    }

    @Log(value = "清空操作日志", isSave = true, operType = OperatorType.CLEAR)
    @PreAuthorize(hasPermission = "operlog:export")
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    public void export(HttpServletResponse response) {
        List<List<String>> exportData = SysLogService.export();
        ExcelUtil.uploadExcelAboutUser(response, "操作日志", Arrays.asList(LogConstant.OPERATOR_LOG_COLUMN), exportData);
    }

}
